<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import bs from 'better-scroll'
  export default {
    props: {
      probeType: {
        type: Number,
        default: 0
      },
      pullUpLoad: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        scroll: null
      }
    },
    mounted(){
      // 1.创建BScroll对象
      this.scroll = new bs(this.$refs.wrapper,{
        click: true,
        observeDOM: true,
        observeImage: true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad
      }),
      // console.log(this.scroll);
      // 2.动态监听滚动位置
      this.scroll.on('scroll',(position)=>{
        // console.log(position);
        // 发送给Home
        this.$emit('scroll',position)
      })

      // 3.监听上拉事件
      this.scroll.on('pullingUp',()=>{
        this.$emit('pullingUp')
      })
    },
    methods: {
      // 封装自定义方法，实现自定义化
      scrollTo(x,y,time=500){
        // 这里调用sroll对象的scrollTo方法
        this.scroll && this.scroll.scrollTo(x,y,time)
      },
      finishPullUp(){
        this.scroll && this.scroll.finishPullUp()
      },
      refresh(){
        // console.log('---');
        this.scroll && this.scroll.refresh()
      },
      // 获取组件离开时的位置
      getScrollY(){
        return this.scroll.y ? this.scroll.y : 0
      }
    }
  }
</script>

<style scoped>
</style>